package com.jsu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jsu.mapper.AgendaMapper;
import com.jsu.mapper.AgendaSubMapper;
import com.jsu.service.AgendaService;
import com.jsu.service.AgendaSubService;
import org.example.model.activity.pojo.AgendaSub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Service
public class AgendaSubImpl implements AgendaSubService {

    @Autowired
    private AgendaSubMapper agendaSubMapper;
    //增加用户订阅
    @Transactional
    public void updateAgendaSub(int agendaId, int userId,int isSubscribed){
        LambdaQueryWrapper<AgendaSub> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AgendaSub::getAgendaId,agendaId)
                .eq(AgendaSub::getUserId,userId);
        AgendaSub agendaSub1 = agendaSubMapper.selectOne(lambdaQueryWrapper);
        //为点赞标识
        if (isSubscribed==1){
            if (Objects.isNull(agendaSub1)){
                AgendaSub agendaSub = new AgendaSub();
                agendaSub.setAgendaId(agendaId);
                agendaSub.setUserId(userId);
                agendaSub.setSubscribed(1);
                Date date = new Date();
                agendaSub.setSubscriptionTime(date);
                agendaSubMapper.insert(agendaSub);
            }
            else {
                throw new RuntimeException("无法重复点赞");
            }
        }
        //为取消标识
        else {
            agendaSub1.setSubscriptionTime(new Date());
            agendaSub1.setSubscribed(0);
            agendaSubMapper.updateById(agendaSub1);
        }

    }
}
